 /*
 * Kotak terdiri dari berbagai jenis, seperti Lokasi Pariwisata, airport, kotak biasa, dana umum, maupun kesempatan
 * 
 */

package model.board;

import helper.XMLReader;
import java.io.Serializable;
import model.players.*;

public class Kotak implements Serializable
{
    //Attribute
    protected int posisi = 0;
    protected int tipe = 0;

    protected String nama = "";
    protected String kompleks = "";
    protected Player owner = null;
    protected String icon = "";
    protected int hargabeli = 0;
    protected int hargasewa = 0;

    //Constructor
    public Kotak ()
    {}

    public Kotak (String nama)
    {
        this.nama = nama;
    }

    public Kotak newKotak (int _posisi)
    {
        Kotak retval = new Kotak();
        switch (_posisi)
        {
            case 0:
                retval = new Kotak();
                retval.tipe = 0;
                break;
            case 1:
                retval = new Kotak("Start");
                break;
            case 2:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 3:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 4:
                retval = new Kotak("Community Chest");
                retval.tipe = 3;
                break;
            case 5:
                retval = new Airport(_posisi);
                retval.tipe = 4;
                break;
            case 6:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 7:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 8:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 9:
                retval = new Kotak ("Jail");
                retval.tipe = 5;
                break;
            case 10:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 11:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 12: //KESEMPATAN
                retval = new Kotak("Chance");
                retval.tipe = 9;
                break;
            case 13:
                retval = new Airport(_posisi);
                retval.tipe = 4;
                break;
            case 14:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 15:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 16:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 17:
                retval = new Kotak("Free Parking");
                retval.tipe = 8;
                break;
            case 18: 
                retval = new Kotak("Community Chest");
                retval.tipe = 3;
                break;
            case 19:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 20:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 21:
                retval = new Airport(_posisi);
                retval.tipe = 4;
                break;
            case 22:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 23:
                retval = new Kotak("Skip");
                retval.tipe = 11;
                break;
            case 24:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 25:
                retval = new Kotak("Go To Jail");
                retval.tipe = 6;
                break;
            case 26:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 27:
                retval = new Kotak("Reverse");
                retval.tipe = 10;
                break;
            case 28:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 29:
                retval = new Airport(_posisi);
                retval.tipe = 4;
                break;
            case 30:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;
            case 31:
                retval = new Kotak("Chance");
                retval.tipe = 9;
                break;
            case 32:
                retval = new LokasiPariwisata(_posisi);
                retval.tipe = 2;
                break;

        }
        retval.posisi = _posisi;
        return retval;
    }
    //Getter
    //posisi = 0;
    public int getPosisi()
    {
        return posisi;
    }
    //tipe = 0
    public int getTipe()
    {
        return tipe;
    }

    public String getIcon()
    {
         return ("LPimages/iconLP"+this.getPosisi()+".jpg");
    }
    
    //nama = ""
    public String getNama()
    {
        return nama;
    }
    //kompleks = ""
    public String getKompleks()
    {
        return kompleks;
    }
    //owner = null
    public Player getOwner()
    {
        return owner;
    }
    //hargabeli = 0
    public int getHargabeli()
    {
        return hargabeli;
    }
    //hargasewa = 0;
    public int getHargasewa()
    {
        return hargasewa;
    }

    //hargajual = 0
    public int getHargajual()
    {
        return hargabeli/2;
    }

    //Setter

    //posisi = 0;
    public void setPosisi(int _posisi)
    {
        posisi = _posisi;
    }
    //    tipe = 0;
    public void setTipe (int _tipe)
    {
        tipe = _tipe;
    }
    //    nama = "";
    public void setNama (String _nama)
    {
        nama = _nama;
    }
    //    kompleks = "";
    public void setKompleks (String _kompleks)
    {
        kompleks = _kompleks;
    }
    //    owner = null;
    public void setOwner (Player _owner)
    {
        owner = _owner;
    }
    //    hargabeli = 0;
    public void setHargabeli (int _harga)
    {
        hargabeli = _harga;
    }

    public void setHargasewa (int _harga)
    {
        hargasewa = _harga;
    }

    //method print atribut untuk airport
    public String PrintAttribute()
    {
        String retval = new String ();
        retval = retval.concat("Name \t:" + nama);
        if (tipe == 4) 
        {
            if (owner != null)
                retval = retval.concat("\n\nOwner \t:"+owner.getNama());
            retval = retval.concat("\n\nBuying Price\t:" +hargabeli);
            retval = retval.concat("\n\nRenting Price\t:" +hargasewa);
        }
        return retval;
    }

    //method beli
    public String BeliAset (Player current_plyr)
    {
        String retval = new String ("");
        //KONDISI : BELUM ADA YANG PUNYA
        if (this.owner == null)
        {
             //KONDISI : CUKUP BUAT BELI
            if (current_plyr.bayar(hargabeli))
            {
                this.owner = current_plyr;
                current_plyr.jum_aset += 1;
                current_plyr.aset[current_plyr.jum_aset] = posisi;
                retval = new String (current_plyr.getNama()+ " has just bought " +nama+ " for " +hargabeli );
            }
             //KONDISI : TIDAK CUKUP UANG UNTUK BELI
            else
                retval = new String (current_plyr.getNama()+" tried to buy "+nama+" but there's no sufficient fund.");
        }
       return retval;
    }

    //method sewa
    public String SewaAset (Player current_plyr)
    {
        String retval = new String ("");
        //KONDISI : SUDAH ADA YANG PUNYA
        if (this.owner != null)
        {
             //KONDISI : SANGGUP BAYAR
            if (current_plyr.bayar(hargasewa))
            {
                getOwner().addUang(hargasewa);
                retval = new String (current_plyr.getNama()+" has paid rent Rp."+hargasewa+" to "+getOwner().getNama());
            }
            //KONDISI : TIDAK SANGGUP BAYAR
            else
                retval = new String ("Payment has failed. Not sufficient fund");
        }
        return retval;
    }

    //==============================================================================

    public static void puts (String S)
    {
        System.out.println(S);
    }

    //=============================================================================
    //MAIN DRIVER
    
    public static void main (String args[])
    {

        if (XMLReader.parseToArrLP("lokasi_pariwisata.xml", 18))
        {
            Kotak[] Board = new Kotak[33];
            for (int i = 1; i<=32; i++)
            {
                Board[i] = new Kotak();
                Board[i] = Board[i].newKotak(i);
            }

            Kotak airport = Board[5];

            puts(airport.PrintAttribute());
            
            airport.setNama("airport baru");
            airport.setKompleks("kompleks baru");
            airport.setPosisi(10);
            airport.setTipe(3);
            airport.setHargabeli(1000);
            airport.setHargasewa(500);

            puts("\nNama: "+airport.getNama()+" Kompleks: "+airport.getKompleks()+" Posisi: "+airport.getPosisi()+" Tipe: "+airport.getTipe()+" Pemilik: "+airport.getOwner());
            puts("Harga beli: "+airport.getHargabeli()+" Harga sewa: "+airport.getHargasewa()+" Harga jual: "+airport.getHargajual());
            
        }
    }
}
